﻿using System.Diagnostics;

namespace DimensionsHelper.LanguageService.Syntax;

public class EnumMemberSignature : Declaration
{
    private SyntaxToken? _equalToken;
    private IntegerLiteral? _initialValue;

    internal EnumMemberSignature(Identifier name, SyntaxNode? parent) :
        base(SyntaxKinds.EnumMemberSignature, SyntaxToken.Default, name, parent)
    {
    }


    public override bool IsSignature => true;


    /// <summary>
    ///     '='等号符号Token
    /// </summary>
    public SyntaxToken? EqualToken => _equalToken;


    /// <summary>
    ///     手动指定的初始值
    /// </summary>
    public IntegerLiteral? InitialValue => _initialValue;


    /// <summary>
    ///     枚举类型值，此属性值由符号绑定初始化
    /// </summary>
    public int Value { get; set; }


    protected override string GetFullName()
    {
        Debug.Assert(Parent != null);
        return $"{((EnumSignature)Parent).FullName}.{Name}";
    }


    public void Update(SyntaxToken equalToken, IntegerLiteral value)
    {
        SetChildNullableField(ref _equalToken, SyntaxToken.IsNullOrEmpty, equalToken);
        SetChildNullableField(ref _initialValue, IntegerLiteral.IsEmptyOrDefault, value);
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, NameIdentifier, EqualToken, InitialValue);
    }
}


public static partial class NodeFactory
{
    public static EnumMemberSignature CreateEnumMemberSignature(SyntaxNode? parent, Identifier name,
        SyntaxToken? equalToken, IntegerLiteral? value)
    {
        EnumMemberSignature signature = new(name, parent);

        if (!IntegerLiteral.IsEmptyOrDefault(value) && !SyntaxToken.IsNullOrEmpty(equalToken))
        {
            signature.Update(equalToken, value);
        }

        return signature;
    }
}